package com.ddwl.data.service;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.exception.ParamException;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.common.util.UUIDGenerator;
import com.ddwl.data.dao.ChannelConfigDao;
import com.ddwl.schema.bo.data.ChannelConfigAddBo;
import com.ddwl.schema.bo.data.ChannelConfigPageBo;
import com.ddwl.schema.bo.data.ChannelConfigUpdateBo;
import com.ddwl.schema.bo.data.ChannelConfigValidBo;
import com.ddwl.schema.vo.data.ChannelConfigVo;
import com.ddwl.data.model.ChannelConfig;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.vo.base.PageVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author hemiaojun
 * @Date 2020/11/9 14:54
 */
@Service
public class ChannelConfigService {

    @Resource
    private ChannelConfigDao channelConfigDao;


    public void add(ChannelConfigAddBo bo) {
        ChannelConfig exist = getByCode(bo.getCode());
        ParamException.isNotNull(exist, "已经存在该编码的渠道");
        ChannelConfig entity = BeanConverUtils.convertBean(bo, ChannelConfig.class);
        entity.setValid(BaseEnum.StatusEnum.ENABLE.getCode());
        entity.setId(UUIDGenerator.getShortUUID());
        channelConfigDao.insert(entity);
    }

    public void update(ChannelConfigUpdateBo bo) {
        ChannelConfig exist = getByCode(bo.getCode());
        ParamException.isTrue((null != exist && !exist.getId().equals(bo.getId())), "已经存在该编码的渠道");
        ChannelConfig entity = BeanConverUtils.convertBean(bo, ChannelConfig.class);
        channelConfigDao.updateByPrimaryKeySelective(entity);
    }

    private ChannelConfig getById(String id) {
        return channelConfigDao.selectByPrimaryKey(id);
    }

    public ChannelConfigVo get(String id) {
        return BeanConverUtils.convertBean(getById(id), ChannelConfigVo.class);
    }

    private ChannelConfig getByCode(String code) {
        ChannelConfig entity = new ChannelConfig();
        entity.setCode(code);
        return channelConfigDao.selectOne(entity);
    }

    public void delete(String id) {
        channelConfigDao.deleteById(id);
    }

    public PageVo<ChannelConfigVo> page(PageBo<ChannelConfigPageBo> pageBo) {
        PageHelper.startPage(pageBo.getPage(), pageBo.getPageSize(), MySqlUtil.orderBy(pageBo.getSort(), pageBo.isAsc()));
        PageInfo<ChannelConfig> info = new PageInfo(channelConfigDao.getList(pageBo.getParam()));
        List<ChannelConfigVo> list = BeanConverUtils.convertBeanList(info.getList(), ChannelConfigVo.class);
        return new PageVo<>(info.getTotal(), pageBo.getPage(), pageBo.getPageSize(), list);

    }

    public void changeValid(ChannelConfigValidBo bo) {
        ChannelConfig entity = BeanConverUtils.convertBean(bo, ChannelConfig.class);
        channelConfigDao.updateByPrimaryKeySelective(entity);
    }

    public void changeId() {
        List<ChannelConfig> list = channelConfigDao.getAll();
        if (!CollectionUtils.isEmpty(list)) {
            Map<String, String> map = new HashMap<>();
            for (ChannelConfig cc : list) {
                String id = cc.getId();
                if (id.length() == 11) {
                    long longId = UUIDGenerator.reverseShortUUID(id);
                    map.put(id, String.valueOf(longId));
                }
            }
            if (!CollectionUtils.isEmpty(map)) {
                channelConfigDao.changeId(map);
            }
        }

    }
}
